package com.owen.java.demo.m3u8.manage;

import com.iheartradio.m3u8.*;
import com.iheartradio.m3u8.data.MediaPlaylist;
import com.iheartradio.m3u8.data.Playlist;
import com.iheartradio.m3u8.data.TrackData;
import com.iheartradio.m3u8.data.TrackInfo;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class Download {
    public static void main(String[] args) throws IOException, ParseException, PlaylistException {
        TrackData trackData = new TrackData.Builder()
                .withTrackInfo(new TrackInfo(3.0f, "test"))
                .withUri("https://play.bo159159.com/20191121/LzdZm7YO//700kb/hls/index.m3u8")
                .build();

        List<TrackData> tracks = new ArrayList<>();
        tracks.add(trackData);

        MediaPlaylist mediaPlaylist = new MediaPlaylist.Builder()
                .withMediaSequenceNumber(1)
                .withTargetDuration(3)
                .withTracks(tracks)
                .build();

        Playlist playlist = new Playlist.Builder()
                .withCompatibilityVersion(5)
                .withMediaPlaylist(mediaPlaylist)
                .build();

        OutputStream outputStream = new FileOutputStream("d:\\aaa.txt");

        PlaylistWriter writer = new PlaylistWriter(outputStream, Format.EXT_M3U, Encoding.UTF_8);
        writer.write(playlist);

        writer = new PlaylistWriter.Builder()
                .withOutputStream(outputStream)
                .withFormat(Format.EXT_M3U)
                .withEncoding(Encoding.UTF_8)
                .build();

        writer.write(playlist);

    }
}
